package fire.and.near.challenger;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.facebook.Response;
import com.facebook.model.GraphUser;

import fire.and.near.challenger.database.FriendModel;
import fire.and.near.challenger.database.TableConstants.FriendsTable;
import fire.and.near.challenger.facebook.FacebookActionAdapter;
import fire.and.near.challenger.facebook.FacebookHelper;
import fire.and.near.challenger.web.HttpRequestSender;

public class FriendPickerActivity extends ActionBarActivity {

	public static final int FRIEND_REQUEST_CODE = 58;
	
	private List<FriendModel> friendsList = null;
	private FacebookHelper mFbHelper = null;
	private ProgressBar progressSpinner = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_friend_picker);

		this.progressSpinner = (ProgressBar) findViewById(R.id.progress_spinner);
		this.mFbHelper = LoginActivity.mFbHelper;
		loadFriendsList();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		// getMenuInflater().inflate(R.menu.friend_picker, menu);
		return true;
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (HttpRequestSender.isNetworkAvailable(this)) {
			this.mFbHelper.onActivityResult(this, requestCode, resultCode, data);
		}
	}

	private void loadFriendsList() {
		this.progressSpinner.setVisibility(View.VISIBLE);
		
		Log.i("E for EXCEPTION", Boolean.toString(mFbHelper.isSessionActive()));
		
		if (!HttpRequestSender.isNetworkAvailable(this)) {
			HttpRequestSender.connectingToServerErrorMessage(this);
			finish();
		}
		mFbHelper.getFriends(this, new FacebookActionAdapter() {
			@Override
			public void onGetFriends(List<GraphUser> users, Response response) {
				friendsList = new ArrayList<FriendModel>();
				if (response.getError() == null) {
					for (GraphUser user : users) {
						friendsList.add(new FriendModel(
								Long.parseLong(user.getId()), user.getName()));
					}
					progressSpinner.setVisibility(View.GONE);
					displayFriendsList();
				}
				
				progressSpinner.setVisibility(View.GONE);
			}
		});
	}

	private void displayFriendsList() {
		ListView listViewFriends = (ListView) findViewById(R.id.listView_friends);
		FriendPickerListAdapter friendsAdapter = new FriendPickerListAdapter(this,
				R.id.listView_friends, this.friendsList);
		listViewFriends.setAdapter(friendsAdapter);
		listViewFriends.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent();
				
				Bundle extras = new Bundle();
				extras.putString(FriendsTable.NAME, friendsList.get(position)
						.getName());
				extras.putLong(FriendsTable.FB_ID, friendsList.get(position)
						.getFbID());
				
				intent.putExtras(extras);				
				setResult(RESULT_OK, intent);
				finish();
			}
		});
	}
}
